<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.name" placeholder="请输入合同单号"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="getListData">查询</el-button>
                </el-form-item>
                <!--<el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>-->
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="listData" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="40">
            </el-table-column>
            <el-table-column type="index" label="序号" width="70">
            </el-table-column>
            <el-table-column prop="sn" label="合同单号" width="150" fixed sortable>
            </el-table-column>
            <el-table-column prop="customer" label="合同客户" width="150" sortable>
            </el-table-column>
            <el-table-column prop="signTime" label="签订时间" width="150" sortable>
            </el-table-column>
            <el-table-column prop="employee_id" label="营销人员" width="150" sortable>
            </el-table-column>
            <el-table-column prop="sum" label="合同金额" width="150" sortable>
            </el-table-column>

            <el-table-column prop="intro" label="合同摘要" width="150" sortable>
                <template scope="scope">
                    <el-button size="small" type="button" @click="openIntro(scope.$index, scope.row)">点击查看</el-button>
                </template>
            </el-table-column>

            <el-table-column prop="contractDetails_id" label="付款详情" :formatter="formatDetails" width="150" sortable>
            </el-table-column>

            <el-table-column prop="tenant_id" label="所属租户" width="150" sortable>
            </el-table-column>

            <el-table-column label="操作" fixed="right" width="200">
                <template scope="scope">
                    <el-button size="small" type="danger" @click="handleDel(scope.$index, scope.row)">删除合同</el-button>
                    <el-button size="small" type="primary" @click="saveContract(scope.$index, scope.row)">保存合同</el-button>
                </template>
            </el-table-column>

        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>



    </section>
</template>

<script>
    import util from '../../../common/js/util'
    //import NProgress from 'nprogress'
    import {getUserListPage, removeUser, batchRemoveUser, editUser, addUser} from '../../../api/api';

    export default {
        data() {
            return {
                filters: {
                    name: ''
                },
                listData: [],
                total: 0,
                keyword: '',
                pageSize: 10,
                currentPage: 1,
                listLoading: false,
                sels: [],//列表选中列

                //是否显示付款详情
                /*contractDetailsFormVisible: false,

                //付款详情数据
                contractDetailsForm: {
                    id: 0,
                    sn: '',
                    customer:'',
                    employee_id:'',
                    sum:'',
                    tenant_id:'',
                    intro:'',

                },*/

            }
        },
        methods: {
            //保存合同
            saveContract(inde,row){
                let para = {
                    sn: row.sn,
                    employee_id: row.employee_id,
                    tenant_id: row.tenant_id
                };
                this.listLoading = true;
                this.$http.post('/guarantee/add',para).then(res=>{
                    let ajaxResult =  res.data
                    if (ajaxResult.success){
                        this.$message({
                            message: '保存合同成功',
                            type: 'success'
                        });
                    }else {
                        this.$message({
                            message: '保存合同失败',
                            type: 'error'
                        });
                    }
                    this.listLoading = false;
                }).catch(error=>{
                    console.debug(error)
                    this.listLoading = false;
                    this.$message({
                        message: '保存合同失败',
                        type: 'error'
                    });
                })

            },


            //付款详情显示转换
            formatDetails: function (row, column) {
                return row.contractDetails_id == 1 ? '已付款' : row.contractDetails_id == 0 ? '未付款' : '未知';
            },
            //查看摘要
            openIntro(index, row) {
                this.$alert(row.intro, '订单摘要', {
                    confirmButtonText: '确定',
                })
            },

            handleCurrentChange(val) {
                this.currentPage = val;
                this.getListData();
            },
            //获取用户列表
            getListData() {
                let para = {
                    currentPage: this.currentPage,
                    pageSize:this.pageSize,
                    keyword: this.filters.name
                };
                this.listLoading = true;
                this.$http.post('/contract/findByPage',para).then(res=>{
                    let ajaxResult =  res.data
                    if (ajaxResult.success){
                        this.listData = ajaxResult.data.result;
                        this.total = ajaxResult.data.total
                    }else {
                        this.$message({
                            message: '加载失败',
                            type: 'error'
                        });
                    }
                    this.listLoading = false;
                }).catch(error=>{
                    this.listLoading = false;
                    this.$message({
                        message: '加载失败',
                        type: 'error'
                    });
                })
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    this.$http.delete('/contract/delete/' + row.id, {}).then(res => {
                        let ajaxResult = res.data
                        if (ajaxResult.success) {
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.getListData();
                        } else {
                            this.$message({
                                message: '删除失败',
                                type: 'error'
                            });
                        }
                        this.listLoading = false;
                    }).catch(error => {
                        this.listLoading = false;
                        this.$message({
                            message: '删除失败',
                            type: 'error'
                        });
                    })

                });
            },

            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id)
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = { ids: ids };
                    this.$http.post("/contract/batchDelete",para).then(res=>{
                        let ajaxResult = res.data;
                        if (ajaxResult.success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.getListData();
                        }else {
                            this.$message({
                                message: '删除失败',
                                type: 'error'
                            });
                        }
                        this.listLoading = false;
                    }).catch(error => {
                        this.listLoading = false;
                        this.$message({
                            message: '删除失败',
                            type: 'error'
                        });
                    })

                })
            }
        },
        mounted() {
            this.getListData();
        }
    }

</script>

<style scoped>

</style>